package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.accumulated;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AppntModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * hct019豁免险累计风险
 * @author Jacob
 * @date 2019-12-17 14:28:06
 */
public class Hct019AccumulatedStrategy implements AccumulatedStrategy {

    @Override
    public InsuredModel checkAccumulatedRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                        ProductModel productModel, RedisService redisService, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        if (StringUtils.equals(AcceptConstant.PRODUCT_RISKCODE_HCT019, productModel.getProdCode())) {
            BigDecimal nowTotal = BigDecimal.valueOf(0);
            List<ProductModel> productModelList = requestModel.getProductInfo().getProducts().getProduct();
            StringBuilder sb = new StringBuilder(16);
            for(ProductModel product : productModelList) {
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(product.getProdCode());
                Object obj = redisService.get(sb.toString());
                if (obj != null) {
                    String[] a = String.valueOf(obj).split("\\|");
                    if (StringUtils.equals(a[4], "1") || StringUtils.equals(a[4], "2")) {
                       // if(productConfig.getAccumulatedRiskList().contains(product.getProdCode())) {
                        if (AcceptUtil.isStrategy(StrategyConstant.CRITICAL_ILLNESS_INSURANCE,
                                product.getProdCode(), productConfig.getAccumulatedRiskList())) {
                            nowTotal = nowTotal.add(product.getAmnt().multiply(BigDecimal.valueOf(product.getPayYear())));
                        }
                    }
                }
            }
            AppntModel appntModel = requestModel.getCustInfo().getAppnt();
            BigDecimal cacheTotal = redisService.getCacheQuota(QuotaConstant.SORT_B, appntModel.getIdNo(), nowTotal, requestModel.getPolicyInfo().getContNo());
            //校验投保人累计重疾险保额是否超标
            double historyTotal = insuredCoreRepository.getSeriousDiseaseInsuranceCount(appntModel.getAppntNo(), appntModel.getAppntNo());
            BigDecimal total  = nowTotal.add(BigDecimal.valueOf(historyTotal)).add(cacheTotal);
            Date birthday = appntModel.getAppntBirthday();
            Date cValiDate = requestModel.getPolicyInfo().getCvalidate();
            //获取日期相差的天数
            int days = DateUtil.getDays(birthday, cValiDate);
            int years = DateUtil.getYears(birthday, cValiDate);
            
             Date valiDate = requestModel.getPolicyInfo().getPolApplyDate();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date dateSpecialStart = null;
            Date dateSpecialEND = null;
            try {
                dateSpecialStart = format.parse("2020-01-01");
                dateSpecialEND = format.parse("2020-05-31");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String result;
            if (!valiDate.before(dateSpecialStart) && !valiDate.after(dateSpecialEND)) {
                if (StringUtils.isNotBlank(result = this.specialPromotion(total, days, years, appntModel.getAppntName()))) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, result);
                }
            } else {
                //  30天40岁，50万
                if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FORTY_YEAR
                        && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.FOUR_HUNDRED_THOUSAND)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0020_HCT019_MESSAGE_TWO, appntModel.getAppntName(), years));
                }
                // 41-45岁，30万
                else if (years >= UnderWriteConstant.AGE_FORTY_ONE_YEAR && years <= UnderWriteConstant.AGE_FORTY_FIVE_YEAR
                        && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.THREE_HUNDRED_THOUSAND)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0021_HCT019_MESSAGE, appntModel.getAppntName(), years));
                }
                // 46-50岁，20万
                else if (years >= UnderWriteConstant.AGE_FORTY_SIX_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR
                        && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.ONE_HUNDRED_THOUSAND)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0022_HCT019_MESSAGE_TWO, appntModel.getAppntName(), years));
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }

    /**
     * lj
     * */
    private String specialPromotion(BigDecimal total, int days, int years, String appntName) {
        // 真心真意主险和两款附加险的保额：46-50岁 保额调整为30万
        //  30天40岁，50万
        if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FORTY_YEAR
                && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.FIVE_HUNDRED_THOUSAND)) == 1) {
            return String.format(UnderWriteConstant.UNDER_WRITE_NUGG0020_HCT019_MESSAGE, appntName, years);
        }
        // 41-45岁，30万
        else if (years >= UnderWriteConstant.AGE_FORTY_ONE_YEAR && years <= UnderWriteConstant.AGE_FORTY_FIVE_YEAR
                && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.THREE_HUNDRED_THOUSAND)) == 1) {
            return String.format(UnderWriteConstant.UNDER_WRITE_NUGG0021_HCT019_MESSAGE, appntName, years);
        }
        // 46-50岁，30万
        else if (years >= UnderWriteConstant.AGE_FORTY_SIX_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR
                && AcceptUtil.compareTo(total, BigDecimal.valueOf(UnderWriteConstant.THREE_HUNDRED_THOUSAND)) == 1) {
            return String.format(UnderWriteConstant.UNDER_WRITE_NUGG0022_HCT019_MESSAGE, appntName, years);
        }
        return "";
    }
}
